﻿@using StackExchange.Opserver.Data.Elastic
@model ElasticCluster
@{
    var c = Model;
    var nodes = c.Nodes.SafeData(true).Nodes;
}
<div class="item @c.MonitorStatus.Class()">
    <div class="server-name" title="Last Updated: @c.LastPoll.ToRelativeTime()">
        @c.IconSpan()
        <a href="/elastic?node=@c.Name" class="node-name-link">@c.Name</a>
        <span class="category">@((nodes != null ? nodes.Count : 0).Pluralize("node"))</span>
    </div>
    <div>
        @if (nodes != null && nodes.Any())
        {
            var ss = c.ShardStates;
            <table style="width: 100%;">
                <thead>
                    <tr>
                        <th>Node</th>
                        <th>Shards</th>
                        <th>CPU</th>
                        <th>Mem</th>
                        <th>Idx</th>
                    </tr>
                </thead>
                <tbody>
                    @foreach (var n in nodes)
                    {
                        var shards = ss.Where(sh => sh.Node == n.GUID);
                        <tr>
                            <td>@n.Name</td>
                            <td>@shards.Count().ToComma()</td>
                            @if (n.Stats != null && n.Stats.Process != null && n.Stats.Process.CPU != null)
                            {
                            <td>@((n.Stats.OS.CPU.User + n.Stats.OS.CPU.System).ToComma())%</td>
                            }
                            else
                            {
                            <td class="note">n/a</td>
                            }
                            @if (n.Stats != null && n.Stats.Process.Memory != null)
                            {
                                <td>@n.Stats.Process.Memory.ResidentInBytes.ToSize("b")</td>
                            }
                            else
                            {
                            <td class="note">n/a</td>
                            }
                            <td>@c.HealthStatus.Data.Indices.Count.ToComma()</td>
                        </tr>
                    }
                </tbody>
            </table>
        }
        else
        {
            <div class="no-content">No nodes found</div>
        }
    </div>
</div>